<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/5/3
 * Time: 14:59
 */
namespace app\admin\controller;
use app\data\Data;
use app\validate\LinkValidate;
use think\Config;
use think\Request;
use think\File;

/**
 * +--------------------------------------
 * Class Link
 * @package app\admin\controller
 * 友情连接管理
 * +-------------------------------------
 */
class Link extends Check{
    /**
     * 友情连接列表
     */
    public function linkList(){
        $link_model = new Data('link');
        $link_name = input('link_name');
        $page = input('page');
        if(!empty($link_name)){
            $where['link_name'] = ['like','%'.$link_name.'%'];
            $display['link_name'] = $link_name;
        }
        $result = $link_model->pageSelf($where,'','',$display,'',$page);
        return view('linkList',['result'=>$result,'display'=>$display]);
    }

    /**
     * 添加友情链接
     */
    public function linkAdd(){
        $link_model = new Data('link');
        $link_validate = new LinkValidate();
        try{
            if(Request::instance()->isPost()){
                $post = input('param.');
                $result = $link_model->selectData("link_name = '".trim($post['link_name'])."'");
                if($result){
                    throw new \LogicException(Config::get('error_table.link_name')['msg'],Config::get('error_table.link_name')['code']);
                }
                $res = $link_model->selectData("link_url = '".trim($post['link_url'])."'");
                if($res){
                    throw new \LogicException(Config::get('error_table.link_url')['msg'],Config::get('error_table.link_url')['code']);
                }
                $link_validate = $link_validate->linkValidate();
                self::validateData($link_validate['rule'],$link_validate['message'],$post);
                $post['create_time'] = time();
                $rs = $link_model->insertData($post);
                if(!$rs){
                    throw new \LogicException(Config::get('error_table.error')['msg'],Config::get('error_table.error')['code']);
                }
                return self::formatSuccessResult();
            }
        }catch(\Exception $e){
            return self::formatResult($e->getCode(),$e->getMessage());
        }
        return view('linkAdd');
    }
    /**
    改变友情链接状态
     */
    public function changeAdminStatus(){
        try{
            $satus = intval(input('param.status'));
            $id    = intval(input('param.id'));
            $link_model = new Data('link');
            $status = $satus == 1 ? 2 : 1;
            $rs = $link_model->updateData(['status'=>$status],'id ='.$id);
            if($rs===false){
                throw new \LogicException(Config::get('error_table.error')['msg'],Config::get('error_table.error')['code']);
            }
        }catch(\Exception $e){
            return self::formatResult($e->getCode(),$e->getMessage());
        }
        return self::formatSuccessResult(['status'=>$status]);
    }
    /**
     * 更新友情链接
     */
    public function linkSave(){
        $link_model = new Data('link');
        $link_validate = new LinkValidate();
        try{
            $id = intval(input('id'));
            if(Request::instance()->isPost()){
                $post = input('param.');
                $link_validate = $link_validate->linkValidate();
                self::validateData($link_validate['rule'],$link_validate['message'],$post);
                $rs = $link_model->updateData($post);
                if($rs === false){
                    throw new \LogicException(Config::get('error_table.error')['msg'],Config::get('error_table.error')['code']);
                }
                return self::formatSuccessResult();
            }
        }catch(\Exception $e){
            return self::formatResult($e->getCode(),$e->getMessage());
        }
        $result = $link_model->find('id ='.$id);
        return view('linkSave',['result'=>$result]);
    }

    /**
     * 删除友情链接
     */
    public function linkDelete(){
        $link_model = new Data('link');
        $id = intval(input('id'));
        try{
            $rs = $link_model->deleteDaPtaByPrimaryKey($id);
            if($rs === false){
                throw new \LogicException(Config::get('error_table.error')['msg'],Config::get('error_table.error')['code']);
            }
        }catch(\Exception $e){
            return self::formatResult($e->getCode(),$e->getMessage());
        }
        return self::formatSuccessResult();
    }
}
